package com.example.neusoft.yuejian_12.Adapter;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.example.neusoft.yuejian_12.JavaBean.Club;
import com.example.neusoft.yuejian_12.R;

import java.util.List;

/**
 * Created by admin on 2017/10/25.
 */

public class VideoDetailAdapter  extends RecyclerView.Adapter {
    private static int UpUI = 1;
    private Context context;
    private VideoViewHolder videoViewHolder;
    List<Club.ClubvideoEntity> list;

    public VideoDetailAdapter(Context context, List<Club.ClubvideoEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_video, viewGroup, false);
        VideoViewHolder videoViewHolder = new VideoViewHolder(view);
        return videoViewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        Club.ClubvideoEntity entity = list.get(i);
        videoViewHolder = (VideoViewHolder) viewHolder;
        videoViewHolder.tiele.setText(entity.getName());
        videoViewHolder.hint.setText(entity.getName());
        //视频地址
        controllerVideo();
        Uri videoURL = Uri.parse("http://172.24.10.175/workout/Uploads/tvideo/video/" + entity.getVideopath());
        videoViewHolder.videoView.setVideoPath(videoURL.toString());
        videoViewHolder.videoView.start();
        UIhandler.sendEmptyMessage(UpUI);
        /*MediaController controller=new MediaController(context);
        videoViewHolder.videoView.setMediaController(controller);
        controller.setMediaPlayer(videoViewHolder.videoView);*/
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {
        VideoView videoView;
        TextView tiele;
        ImageView video_play, video_expand;
        TextView hint, current_time, total_time;
        SeekBar programe;

        public VideoViewHolder(View itemView) {
            super(itemView);
            programe = (SeekBar) itemView.findViewById(R.id.programe);
            video_play = (ImageView) itemView.findViewById(R.id.video_play);
            video_expand = (ImageView) itemView.findViewById(R.id.video_expand);
            videoView = (VideoView) itemView.findViewById(R.id.tea_detail_video);
            tiele = (TextView) itemView.findViewById(R.id.tea_video_title);
            hint = (TextView) itemView.findViewById(R.id.tea_video_hint);
            current_time = (TextView) itemView.findViewById(R.id.current_time);
            total_time = (TextView) itemView.findViewById(R.id.total_time);
        }
    }

    public void controllerVideo() {
        videoViewHolder.video_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (videoViewHolder.videoView.isPlaying()) {
                    videoViewHolder.video_play.setImageResource(R.drawable.ic_zanting);
                    videoViewHolder.videoView.pause();
                    UIhandler.removeMessages(UpUI);
                } else {
                    videoViewHolder.video_play.setImageResource(R.drawable.ic_bofang);
                    videoViewHolder.videoView.start();
                    UIhandler.sendEmptyMessage(UpUI);
                }
            }
        });
        videoViewHolder.programe.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    public void timeFormat(TextView textView, int million) {
        int second = million / 1000;
        int hh = second / 3600;
        int mm = second % 3600 / 60;
        int ss = second % 60;
        String str = null;
        if (hh != 0) {
            str = String.format("%02d:%02d:%02d", hh, mm, ss);
        } else {
            str = String.format("%02d:%02d", mm, ss);
        }
        textView.setText(str);
    }

    private Handler UIhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == UpUI) {
                //得到视频当前的播放时间
                int currentPosition = videoViewHolder.videoView.getCurrentPosition();
                //得到视频当前的播放总时间
                int totalDurlation = videoViewHolder.videoView.getDuration();
                //更新当前播放时间
                timeFormat(videoViewHolder.current_time, currentPosition);
                timeFormat(videoViewHolder.total_time, totalDurlation);
                videoViewHolder.programe.setMax(totalDurlation);
                videoViewHolder.programe.setProgress(currentPosition);
                UIhandler.sendEmptyMessageDelayed(UpUI, 500);
            }

        }
    };
}